import time

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By


def agree():
    # 同意  //android.widget.TextView[@resource-id="com.baidu.wenku:id/tv_agree"]
    driver.find_element(By.XPATH, '//android.widget.TextView[@resource-id="com.baidu.wenku:id/tv_agree"]').click()
    time.sleep(2)
    TouchAction(driver).tap(x=544, y=888)


# 搜索
def search():
    # 点击搜索框 //android.widget.ViewFlipper[@resource-id="com.baidu.wenku:id/vf_text"]
    driver.find_element(By.XPATH, '//android.widget.ViewFlipper[@resource-id="com.baidu.wenku:id/vf_text"]').click()
    time.sleep(2)
    # 输入搜索内容 //android.widget.EditText[@resource-id="com.baidu.wenku:id/h5_search_edit_text_inside"]
    driver.find_element(By.XPATH,
                        '//android.widget.EditText[@resource-id="com.baidu.wenku:id/h5_search_edit_text_inside"]').send_keys(
        "python")
    time.sleep(2)
    # 点击搜索按钮 //android.widget.TextView[@resource-id="com.baidu.wenku:id/h5_search_operate_text"]
    driver.find_element(By.XPATH,
                        '//android.widget.TextView[@resource-id="com.baidu.wenku:id/h5_search_operate_text"]').click()
    time.sleep(2)


# 滑动
def swipe():
    for i in range(5):
        driver.swipe(start_x=500, start_y=1000, end_x=500, end_y=500)
        time.sleep(2)


# 退出搜索结果
def exit():
    # //android.widget.ImageView[@resource-id="com.baidu.wenku:id/search_h5_box_back"]
    driver.find_element(By.XPATH,
                        '//android.widget.ImageView[@resource-id="com.baidu.wenku:id/search_h5_box_back"]').click()
    time.sleep(2)


# 点击消息
def message():
    # //android.widget.LinearLayout[@resource-id="com.baidu.wenku:id/tabs"]/android.widget.RelativeLayout[4]/android.widget.RelativeLayout
    if driver.find_element(By.XPATH,
                           '//android.widget.LinearLayout[@resource-id="com.baidu.wenku:id/tabs"]/android.widget.RelativeLayout[4]/android.widget.RelativeLayout').is_displayed():
        driver.find_element(By.XPATH,
                            '//android.widget.LinearLayout[@resource-id="com.baidu.wenku:id/tabs"]/android.widget.RelativeLayout[4]/android.widget.RelativeLayout').click()
        time.sleep(2)
    else:
        print("消息按钮未显示")
        return False


# 关闭更新弹窗
def close_update():
    # //android.widget.ImageView[@resource-id="com.baidu.wenku:id/dialog_pic_close"]
    if driver.find_element(By.XPATH,
                           '//android.widget.ImageView[@resource-id="com.baidu.wenku:id/dialog_pic_close"]').is_displayed():
        driver.find_element(By.XPATH,
                            '//android.widget.ImageView[@resource-id="com.baidu.wenku:id/dialog_pic_close"]').click()
        time.sleep(2)
    else:
        print("更新弹窗未显示")
        return False


if __name__ == '__main__':
    cps = {
        "appActivity": ".splash.view.activity.WelcomeActivity",
        "platformName": "Android",
        "platformVersion": "12",
        "deviceName": "127.0.0.1:16384",
        "appPackage": "com.baidu.wenku"
    }
    url = "http://127.0.0.1:4723/wd/hub"
    driver = webdriver.Remote(url, cps)
    time.sleep(10)

    agree()
    close_update()
    search()
    swipe()
    exit()
    message()
    driver.quit()
